/* eslint-disable import/no-anonymous-default-export */

import * as constants from './constants'
import { fromJS } from 'immutable'

const defaultState = fromJS({
  topicList: [],
  articleList: [],
  articlePage: 1,
  isShow: false,
  recommdPic: [],
  recommdList: [],
  showScroll: false
})

const changeHomeData = (state, action) => {
  return state.merge({
    topicList: fromJS(action.topicList),
    articleList: fromJS(action.articleList),
    recommdPic: fromJS(action.recommdPic),
    recommdList: fromJS(action.recommdList)
  })
}

const addArticleList = (state,action) =>{
  return state.merge({
    articleList: state.get('articleList').concat(action.list),
    articlePage: action.nextPage
  })
}

export default (state = defaultState, action) => {
  switch (action.type) {
    case constants.DOWNLOAD_SHOW:
      return state.set('isShow', true)
    case constants.DOWNLOAD_HIDE:
      return state.set('isShow', false)
    case constants.CHANGE_HOME_DATA:
      return changeHomeData(state, action)
    case constants.ADD_ARTICLE_LIST:
      return addArticleList(state,action)
    case constants.TOGGLE_TOP_SHOW:
      return state.set('showScroll', action.show)
    default:
      return state
  }
}